

import { createRouter, createWebHistory } from 'vue-router'
import { usePermissionStore } from '@/store/permission'
import Layout from '../layout/index.vue'; // 布局组件



const routes = [
    {
        path: '/',
        component: Layout, // 主布局组件
        redirect: '/login', // 添加默认重定向
        children: [
            {
                path: '/home',
                component: () => import("@/views/home.vue"),
                name: 'home',
                meta: {
                    title: '首页',
                    affix: true,
                    noCache: false, // 是否禁用缓存
                    tagView: true,  // 是否显示标签
                }

            },
            {
                path: '/list',
                component: () => import("@/views/list.vue"),
                name: 'list',
                meta: {
                    title: '藏品总览',
                    parentTitle: '藏品管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path: '/test',

            },
            {
                path: '/addCollection',
                component: () => import("@/views/addCollection.vue"),
                name: 'addCollection',
                meta: {
                    title: '添加藏品',
                    parentTitle: '藏品管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path: '/collectionReview',
                component: () => import("@/views/collectionReview.vue"),
                name: 'collectionReview',
                meta: {
                    title: '藏品审核',
                    parentTitle: '藏品管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path: '/classification',
                component: () => import("@/views/classificationOrangement.vue"),
                name: 'classification',
                meta: {
                    title: '分类维护',
                    parentTitle: '分类管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path: '/userManagement',
                component: () => import("@/views/userOrangement.vue"),
                name: 'userManagement',
                meta: {
                    title: '用户管理',
                    parentTitle: '权限管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            }, 
            {
                path:'/departmentMangement',
                component: () => import("@/views/departmentMangement.vue"),
                name: 'departmentMangement',
                meta: {
                    title: '部门管理',
                    parentTitle: '权限管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path:'/positionManagement',
                component: () => import("@/views/positionManagement.vue"),
                name: 'positionManagement',
                meta: {
                    title: '岗位管理',
                    parentTitle: '权限管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path:'/permissionManagement',
                component: () => import("@/views/permissionManagement.vue"),
                name: 'permissionManagement',
                meta: {
                    title: '角色管理',
                    parentTitle: '权限管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path:'/operationLog',
                component: () => import("@/views/showOperation.vue"),
                name: 'operationLog',
                meta: {
                    title: '操作日志',
                    parentTitle: '权限管理',
                    noCache: false, // 是否禁用缓存
                    tagView: true,   // 是否显示标签 
                }
            },
            {
                path:'/loginOperation',
                component:()=>import("@/views/userLoginOperation.vue"),
                name:'userLoginOperation',
                meta:{
                    title:'登录日志',
                    parentTitle:'日志管理',
                    noCache:false,
                    tagView:true,
                }
            },
            {
                path:'/PersonalInformation',
                component:()=>import("@/views/PersonalInformation.vue"),
                name:'PersonalInformation',
                meta:{
                    title:'个人信息',
                    parentTitle:'个人中心',
                    noCache:false,
                    tagView:true,
                }
            },
            {
                path:'/exhibition',
                component:()=>import("@/views/exhibition.vue"),
                name:'exhibition',
                meta:{
                    title:'展览总览',
                    parentTitle:'展览管理',
                    noCache:false,
                    tagView:true,
                }
            },
            {
                path:'/exhibitionOrder/:id',
                component:()=>import("@/views/exhibition/exhibitionOrder.vue"),
                name:'exhibitionOrder',
                meta:{
                    title:'放票管理',
                    parentTitle:'展览管理',
                    noCache:false,
                    tagView:true,
                }
            },{
                path:'/exhibition/orderMangement/:id',
                component:()=>import("@/views/exhibitionOrder/exhiOrder.vue"),
                name:"exhiOrderMangement",
                meta:{
                    title:'展览订单',
                    parentTitle:'展览管理',
                    noCache:false,
                    tagView:true,
                }
            },
            {
                path:'/action',
                component:()=>import("@/views/action/action.vue"),
                name:'action',
                meta:{
                    title:'活动总览',
                    parentTitle:'活动管理',
                    noCache:false,
                    tagView:true,
                }
            },
            {
                path:'/action/actionMangemnet/:id',
                component:()=>import("@/views/action/actionOrder.vue"),
                name:'actionMangemnet',
                meta:{
                    title:'活动订单',
                    parentTitle:'活动管理',
                    noCache:false,
                    tagView:true,
                }
            }
            // 其他需要展示在布局中的路由...
        ]
    },
    {
        path: '/login',
        component: () => import("../views/loginRegister.vue"),
        name: 'loginRegister',
        meta: { title: '登录' }
    },
    {
        path: '/error',
        component: () => import("../views/error.vue"),
        name: 'error',
        meta: { title: '错误页' }
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const permissionStore = usePermissionStore()
    // 更准确的排除条件
    const excludeTitles = ['登录', '错误页']
    const excludePaths = ['/login', '/404']

    // 记录访问历史
    if (to.meta?.tagView !== false) { // 通过meta控制是否显示标签
        if (!excludeTitles.includes(to.meta.title) &&
            !excludePaths.includes(to.path)) {
            permissionStore.addVisitedView({
                path: to.path,
                fullPath: to.fullPath,
                name: to.name,
                meta: { ...to.meta },
                title: to.meta.title || '未知页面'
            })
        }
    }
    next()
})

export default router